package com.hnkywl.sts.action.portal;

import com.google.gson.Gson;
import com.hnkywl.sts.entity.base.Xtgg;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.listener.OnlineInfo;
import com.hnkywl.sts.listener.UserInfo;
import com.hnkywl.sts.service.base.XtggService;
import com.hnkywl.sts.service.cggl.BgCgdService;
import com.hnkywl.sts.service.cggl.CgdService;
import com.hnkywl.sts.service.htgl.HtService;
import com.hnkywl.sts.service.jhgl.*;
import com.hnkywl.sts.service.jsgl.CgjsdService;
import com.hnkywl.sts.service.jsgl.XsjsdService;
import com.hnkywl.sts.service.kcgl.CkshService;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.sfgl.FkshService;
import com.hnkywl.sts.service.sfgl.SkshService;
import com.hnkywl.sts.service.sys.CdService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.xsgl.KhService;
import com.hnkywl.sts.service.xsgl.XsdService;
import com.hnkywl.sts.util.InterfaceParam;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-3-13
 * Time: 上午11:32
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/main")
public class MainAction extends SimpleActionSupport {

    private YgService ygService;

    private GsjgService gsjgService;

    private CdService cdService;

    private JkdService jkdService;

    private String currentUserName;

    private String currentOragnName;

    private Date currentDate;

    // 协议service
    private XyService xyService;

    // 代办采购协议数
    private String dbcgxyNum;

    // 代办销售协议数
    private String dbxsxyNum;

    // 协议url
    private String xyUrl;

    // 物资计划service
    private WzjhService wzjhService;

    // 物资计划数
    private String wzjhNum;

    // 物资计划url
    private String wzjhUrl;

    // 资金计划service
    private ZjjhService zjjhService;

    // 资金计划数
    private String zjjhNum;

    // 资金计划Url
    private String zjjhUrl;

    // 电子借款单
    private String dzjkdNum;

    //  电子借款单url
    private String dzjkdUrl;

    // 采购单service
    private CgdService cgdService;

    //采购单数
    private String cgdNum;

    // 采购单url
    private String cgdUrl;

    //验收单数
    private String ysdNum;

    //入库单数
    private String rkdNum;

    //  入库单url
    private String rkdUrl;


    // 合同service
    private HtService htService;

    // 采购合同数
    private String cghtdNum;

    // 采购合同url
    private String cghtUrl;

    // 销售合同数
    private String xshtdNum;

    // 销售合同url
    private String xshtUrl;

    // 保供合同数
    private String bghtdNum;

    // 保供合同url
    private String bghtUrl;

    // 保供采购单数
    private String bgcgdNum;

    // 保供采购单url
    private String bgcgdUrl;

    // 保供计划service
    private BgjhService bgjhService;

    // 保供计划数
    private String bgjhNum;

    // 保供计划url
    private String bgjhUrl;

    // 采购结算单service
    private CgjsdService cgjsdService;

    // 采购结算单数
    private String cgjsdNum;

    // 采购结算url
    private String cgjsUrl;

    // 销售结算单
    private XsjsdService xsjsdService;

    // 销售结算单数
    private String xsjsdNum;

    // 销售结算url
    private String xsjsUrl;

    //进项发票
    private String jxfpNum;
    //销项发票
    private String xxfpNum;
    // 发票url
    private String fpUrl;

    // 销售单service
    private XsdService xsdService;

    //出库审核service
    private CkshService ckshservice;

    // 销售单数
    private String xsdNum;

    // 销售单url
    private String xsdUrl;

    //出库单数
    private String ckdNum;

    //出库单url
    private String ckdUrl;

    // 收款service
    private SkshService skshService;

    // 收款单数
    private String skshNum;

    // 收款url
    private String skshUrl;

    // 付款service
    private FkshService fkshService;

    // 付款单数
    private String fkshNum;

    // 付款url
    private String fkshUrl;

    // 库存service
    private KhService khService;

    // 保供采购单
    private BgCgdService bgCgdService;

    // 系统公告
    private List<Xtgg> xtggs;

    private XtggService xtggService;

    private InterfaceParam interfaceParam;

    private String version;

    @Action("index")
    public String index() {
        version = interfaceParam.getVersion();
        currentUserName = YgService.getCurrentYgName();
        currentOragnName = gsjgService.getZgjsJc() + YgService.getCurrentYgGs();
        xtggs = xtggService.findPub();
        currentDate = new Date();
        // 首页显示数字
        //mainNum();
        return SUCCESS;
    }

    @Action("bbs")
    public String bbs() {
        currentUserName = YgService.getCurrentYgName();
        return SUCCESS;
    }

    @Action("createMenu")
    public void createMenu() {
        Set<Long> ygJs = YgService.getCurrentYgQxId();
        if (ygJs != null && ygJs.size() > 0) {
            List<Map> cdList = new ArrayList<Map>();
            cdList.addAll(cdService.findCdByJs(ygJs));
            String menuJson = new Gson().toJson(cdList);
            this.renderText(menuJson);
        } else {
            this.renderText("{}");
        }
    }


    /**
     * 首页显示的数字
     */
    private void mainNum() {
        List<Map> cdList = new ArrayList<Map>();
        Set<Long> ygJs = YgService.getCurrentYgQxId();
        if (ygJs != null && ygJs.size() > 0) {
            cdList = cdService.findCdByJs(ygJs);
        }
        // 已保存待提交采购协议数
        dbcgxyNum = this.xyService.findAllYbcCgXy();
        // 已保存待提交销售协议数
        dbxsxyNum = this.xyService.findAllYbcXsXy();
        // 用户是否有协议菜单
        xyUrl = urlMenu("jhgl/xygl/xygl", cdList);
        // 物资计划数
        wzjhNum = this.wzjhService.findAllWzjh();
        // 用户是否有物资计划菜单
        wzjhUrl = urlMenu("jhgl/wzjhgl/wzjhgl", cdList);
        // 资金计划数
        zjjhNum = this.zjjhService.findAllZjjh();
        // 用户是否有资金计划菜单
        zjjhUrl = urlMenu("jhgl/zjjhgl/zjjhgl", cdList);
        // 电子借款单数
        dzjkdNum = this.jkdService.findAllDzjkd();
        // 电子借款单菜单
        dzjkdUrl = urlMenu("jhgl/jkd/jkd", cdList);
        // 采购单数
        cgdNum = this.cgdService.findAllCgd();
        // 采购单菜单
        cgdUrl = urlMenu("cggl/cgdgl/cgdgl", cdList);
        // 验收单数
        ysdNum = this.cgdService.findAllCgdRkys("false", "false");
        // 入库单数
        rkdNum = this.cgdService.findAllCgdRkys("true", "false");
        // 入库单菜单
        rkdUrl = urlMenu("kcgl/rksh/rksh", cdList);

        // 采购合同数
        cghtdNum = this.htService.findAllCghtd();
        // 采购合同菜单
        cghtUrl = urlMenu("htgl/cghtgl/cghtgl", cdList);
        // 销售合同数
        xshtdNum = this.htService.findAllXshtd();
        // 销售合同菜单
        xshtUrl = urlMenu("htgl/xshtgl/xshtgl", cdList);
//        // 保供合同数
//        bghtdNum = this.htService.findAllBghtd();
//        // 保供合同菜单
//        bghtUrl = urlMenu("htgl/bghtgl/bghtgl");
//        // 保供采购单数
//        bgcgdNum = this.bgCgdService.findAllBgcgd();
//        // 保供采购单url
//        bgcgdUrl = urlMenu("cggl/bgcgdgl/bgcgdgl");
//        // 保供计划数(新增的保供计划)
//        bgjhNum = this.bgjhService.findAllBgjh();
//        // 保供计划菜单
//        bgjhUrl = urlMenu("jhgl/bgjhlr/bgjhlr");
        // 采购结算单数
        cgjsdNum = this.cgjsdService.findAllCgjsd(0, 0);
        // 采购结算菜单
        cgjsUrl = urlMenu("jsgl/cgjsd/cgjsd", cdList);
        // 销售结算单
        xsjsdNum = this.xsjsdService.findAllXsjsd(0, 1);
        // 销售结算菜单
        xsjsUrl = urlMenu("jsgl/xsjsd/xsjsd", cdList);

        // 进项发票数
        jxfpNum = this.cgjsdService.findAllCgjsd(2, 0);
        // 销项发票数
        xxfpNum = this.xsjsdService.findAllXsjsd(2, 1);
        // 发票菜单
        fpUrl = urlMenu("jsgl/fp/fp", cdList);

        //销售单数
        xsdNum = this.xsdService.findAllXsd();
        // 销售单url
        xsdUrl = urlMenu("xsgl/xsdgl/xsdgl", cdList);
        //出库单数
        ckdNum = this.ckshservice.findCkdList();
        // 出库单url
        ckdUrl = urlMenu("kcgl/cksh/cksh", cdList);

        // 收款单数
        skshNum = this.skshService.findAllSksh();
        //收款单url
        skshUrl = urlMenu("/sfgl/sksh/sksh", cdList);
        // 付款单数
        fkshNum = this.fkshService.findAllFksh();
        // 付款单菜单
        fkshUrl = urlMenu("/sfgl/fksh/fksh", cdList);
    }

    // 库存量信息
    @Action("getKcTbsj")
    public void getKcTbsj() {
        // 查询库存
        List kcList = this.khService.findKhGroupGs();

        // x轴数据
        StringBuffer xsb = new StringBuffer();
        xsb.append("[");
        // y轴数据
        StringBuffer ysb = new StringBuffer();
        ysb.append("[");

        if (null != kcList && kcList.size() > 0) {
            for (int i = 0; i < kcList.size(); i++) {
                Object[] objects = (Object[]) kcList.get(i);
                xsb.append("'" + objects[0] + "'");
                ysb.append(objects[1]);
                if (i != kcList.size() - 1) {
                    xsb.append(",");
                    ysb.append(",");
                }
            }
        }
        xsb.append("]");
        ysb.append("]");
        renderText("{ \"listXdata\":" + xsb + " , \"listYdata\":" + ysb + "}");

    }


    // 营业收入
    @Action("getSrTbsj")
    public void getSrTbsj() {
        // x轴数据
        StringBuffer xsb = new StringBuffer();
        xsb.append("[");
        for (int i = 1; i <= 12; i++) {
            if (i < 10) {
                xsb.append("'" + "0" + i + "月" + "'");
            } else {
                xsb.append("'" + i + "月" + "'");
            }
            if (i != 12) {
                xsb.append(",");
            }
        }
        xsb.append("]");

        // 保供业务收入
        String bgywsrData = this.xsdService.findYwsr("bgxs");

        // 经营业务收入
        String jyywsrData = this.xsdService.findYwsr("jyxs");

        renderText("{ \"listXdate\":" + xsb + " , \"listBgywsrYdata\":" + bgywsrData + ",\"listJyywsrYdata\":" + jyywsrData + "}");

    }

    /**
     * 判断用户是否据有该菜单
     */
    private String urlMenu(String url, List<Map> cdList) {
        String urlStr = null;
        for (int i = 0; i < cdList.size(); i++) {
            if (null != cdList.get(i).get("url") && cdList.get(i).get("url").equals(url)) {
                urlStr = cdList.get(i).get("url").toString();
                break;
            }
        }
        return urlStr;
    }

    private Set<Long> initUserRole() {
        Yg currentYg = YgService.getCurrentYg();
        List list = ygService.findYgAllJs(currentYg.getId());
        Set<Long> jsSet = new HashSet<Long>();
        jsSet.addAll(list);
        return jsSet;
    }


    private String createMenuJson(Set<Long> ygJs, List<Object[]> cdList) {

        List<Object[]> bottomCdList = this.cdService.findBottomCdByJs(ygJs);

        StringBuilder menuJson = new StringBuilder("[");
        for (int i = 0; i < cdList.size(); i++) {
            Object[] cd = cdList.get(i);
            if (i != cdList.size() - 1) {
                Object[] cd1 = cdList.get(i + 1);
                if (cd[1] == null || (cd1[1] != null && cd1[1].equals(cd[0]))) {
                    if (menuJson.length() > 1)
                        menuJson = menuJson.deleteCharAt(menuJson.length() - 1).append("]},");
                    menuJson.append("{\"id\":\"").append(cd[0]).append("\",\"text\":\"").append(cd[2]).append("\",\"state\":\"closed\"" + ",\"children\":[");
                } else {
                    menuJson.append("{\"id\":\"").append(cd[0]).append("\",\"text\":\"").append(cd[2]).append("\",\"attributes\":{\"url\":\"")
                            .append(cd[3]).append("\",\"type\":\"").append(cd[4]).append("\"}},");
                }
            } else {
                menuJson.append("{\"id\":\"").append(cd[0]).append("\",\"text\":\"").append(cd[2]).append("\",\"attributes\":{\"url\":\"")
                        .append(cd[3]).append("\",\"type\":\"").append(cd[4]).append("\"}},");
            }
        }
        if (menuJson.length() > 1)
            menuJson = menuJson.deleteCharAt(menuJson.length() - 1).append("]}");
        menuJson.append("]");
        return menuJson.toString();
    }

    @Action(value = "logout", results = {
            @Result(name = "logout", location = "../portal/login", type = "redirect")
    })
    public String logout() {
        YgService.clearCurrentUser();
        this.session.clear();
        return "logout";
    }

    //在线用户
    @Action("onlineuser")
    public String inlineUser() {
        Gson gson = new Gson();
        inlineUsers = OnlineInfo.getUserInfos();
        return SUCCESS;
    }

    private Map<String, UserInfo> inlineUsers;

    public Map<String, UserInfo> getInlineUsers() {
        return inlineUsers;
    }


    public void setInlineUsers(Map<String, UserInfo> inlineUsers) {
        this.inlineUsers = inlineUsers;
    }




    public String getCurrentUserName() {
        return currentUserName;
    }

    public void setCurrentUserName(String currentUserName) {
        this.currentUserName = currentUserName;
    }

    public Date getCurrentDate() {
        return currentDate;
    }

    public void setCurrentDate(Date currentDate) {
        this.currentDate = currentDate;
    }

    public String getCurrentOragnName() {
        return currentOragnName;
    }

    public void setCurrentOragnName(String currentOragnName) {
        this.currentOragnName = currentOragnName;
    }

    public String getDbcgxyNum() {
        return dbcgxyNum;
    }

    public void setDbcgxyNum(String dbcgxyNum) {
        this.dbcgxyNum = dbcgxyNum;
    }

    public String getDbxsxyNum() {
        return dbxsxyNum;
    }

    public void setDbxsxyNum(String dbxsxyNum) {
        this.dbxsxyNum = dbxsxyNum;
    }

    public String getWzjhNum() {
        return wzjhNum;
    }

    public void setWzjhNum(String wzjhNum) {
        this.wzjhNum = wzjhNum;
    }

    public String getZjjhNum() {
        return zjjhNum;
    }

    public void setZjjhNum(String zjjhNum) {
        this.zjjhNum = zjjhNum;
    }

    public String getDzjkdNum() {
        return dzjkdNum;
    }

    public void setDzjkdNum(String dzjkdNum) {
        this.dzjkdNum = dzjkdNum;
    }

    public String getCgdNum() {
        return cgdNum;
    }

    public void setCgdNum(String cgdNum) {
        this.cgdNum = cgdNum;
    }

    public String getYsdNum() {
        return ysdNum;
    }

    public void setYsdNum(String ysdNum) {
        this.ysdNum = ysdNum;
    }

    public String getRkdNum() {
        return rkdNum;
    }

    public void setRkdNum(String rkdNum) {
        this.rkdNum = rkdNum;
    }

    public String getBghtdNum() {
        return bghtdNum;
    }

    public void setBghtdNum(String bghtdNum) {
        this.bghtdNum = bghtdNum;
    }

    public String getCghtdNum() {
        return cghtdNum;
    }

    public void setCghtdNum(String cghtdNum) {
        this.cghtdNum = cghtdNum;
    }

    public String getXshtdNum() {
        return xshtdNum;
    }

    public void setXshtdNum(String xshtdNum) {
        this.xshtdNum = xshtdNum;
    }

    public String getBgjhNum() {
        return bgjhNum;
    }

    public void setBgjhNum(String bgjhNum) {
        this.bgjhNum = bgjhNum;
    }

    public String getCgjsdNum() {
        return cgjsdNum;
    }

    public void setCgjsdNum(String cgjsdNum) {
        this.cgjsdNum = cgjsdNum;
    }

    public String getXsdNum() {
        return xsdNum;
    }

    public void setXsdNum(String xsdNum) {
        this.xsdNum = xsdNum;
    }

    public String getCkdNum() {
        return ckdNum;
    }

    public void setCkdNum(String ckdNum) {
        this.ckdNum = ckdNum;
    }

    public String getCkdUrl() {
        return ckdUrl;
    }

    public void setCkdUrl(String ckdUrl) {
        this.ckdUrl = ckdUrl;
    }

    public String getXsjsdNum() {
        return xsjsdNum;
    }

    public void setXsjsdNum(String xsjsdNum) {
        this.xsjsdNum = xsjsdNum;
    }

    public String getJxfpNum() {
        return jxfpNum;
    }

    public void setJxfpNum(String jxfpNum) {
        this.jxfpNum = jxfpNum;
    }

    public String getXxfpNum() {
        return xxfpNum;
    }

    public void setXxfpNum(String xxfpNum) {
        this.xxfpNum = xxfpNum;
    }

    public String getFpUrl() {
        return fpUrl;
    }

    public void setFpUrl(String fpUrl) {
        this.fpUrl = fpUrl;
    }

    public String getXyUrl() {
        return xyUrl;
    }

    public void setXyUrl(String xyUrl) {
        this.xyUrl = xyUrl;
    }

    public String getWzjhUrl() {
        return wzjhUrl;
    }

    public void setWzjhUrl(String wzjhUrl) {
        this.wzjhUrl = wzjhUrl;
    }

    public String getZjjhUrl() {
        return zjjhUrl;
    }

    public void setZjjhUrl(String zjjhUrl) {
        this.zjjhUrl = zjjhUrl;
    }

    public String getCgdUrl() {
        return cgdUrl;
    }

    public void setCgdUrl(String cgdUrl) {
        this.cgdUrl = cgdUrl;
    }

    public String getRkdUrl() {
        return rkdUrl;
    }

    public void setRkdUrl(String rkdUrl) {
        this.rkdUrl = rkdUrl;
    }

    public String getCghtUrl() {
        return cghtUrl;
    }

    public void setCghtUrl(String cghtUrl) {
        this.cghtUrl = cghtUrl;
    }

    public String getXshtUrl() {
        return xshtUrl;
    }

    public void setXshtUrl(String xshtUrl) {
        this.xshtUrl = xshtUrl;
    }

    public String getBghtUrl() {
        return bghtUrl;
    }

    public void setBghtUrl(String bghtUrl) {
        this.bghtUrl = bghtUrl;
    }

    public String getBgjhUrl() {
        return bgjhUrl;
    }

    public void setBgjhUrl(String bgjhUrl) {
        this.bgjhUrl = bgjhUrl;
    }

    public String getCgjsUrl() {
        return cgjsUrl;
    }

    public void setCgjsUrl(String cgjsUrl) {
        this.cgjsUrl = cgjsUrl;
    }

    public String getXsjsUrl() {
        return xsjsUrl;
    }

    public void setXsjsUrl(String xsjsUrl) {
        this.xsjsUrl = xsjsUrl;
    }

    public String getXsdUrl() {
        return xsdUrl;
    }

    public void setXsdUrl(String xsdUrl) {
        this.xsdUrl = xsdUrl;
    }

    public String getSkshNum() {
        return skshNum;
    }

    public void setSkshNum(String skshNum) {
        this.skshNum = skshNum;
    }

    public String getSkshUrl() {
        return skshUrl;
    }

    public void setSkshUrl(String skshUrl) {
        this.skshUrl = skshUrl;
    }

    public String getFkshNum() {
        return fkshNum;
    }

    public void setFkshNum(String fkshNum) {
        this.fkshNum = fkshNum;
    }

    public String getFkshUrl() {
        return fkshUrl;
    }

    public void setFkshUrl(String fkshUrl) {
        this.fkshUrl = fkshUrl;
    }

    public List<Xtgg> getXtggs() {
        return xtggs;
    }

    public void setXtggs(List<Xtgg> xtggs) {
        this.xtggs = xtggs;
    }

    public String getDzjkdUrl() {
        return dzjkdUrl;
    }

    public void setDzjkdUrl(String dzjkdUrl) {
        this.dzjkdUrl = dzjkdUrl;
    }

    public String getBgcgdNum() {
        return bgcgdNum;
    }

    public void setBgcgdNum(String bgcgdNum) {
        this.bgcgdNum = bgcgdNum;
    }

    public String getBgcgdUrl() {
        return bgcgdUrl;
    }

    public void setBgcgdUrl(String bgcgdUrl) {
        this.bgcgdUrl = bgcgdUrl;
    }

    public String getVersion() {
        return version;
    }

    @Autowired
    public void setCdService(CdService cdService) {
        this.cdService = cdService;
    }

    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }

    @Autowired
    public void setXyService(XyService xyService) {
        this.xyService = xyService;
    }

    @Autowired
    public void setWzjhService(WzjhService wzjhService) {
        this.wzjhService = wzjhService;
    }

    @Autowired
    public void setZjjhService(ZjjhService zjjhService) {
        this.zjjhService = zjjhService;
    }

    @Autowired
    public void setCgdService(CgdService cgdService) {
        this.cgdService = cgdService;
    }

    @Autowired
    public void setHtService(HtService htService) {
        this.htService = htService;
    }

    @Autowired
    public void setBgjhService(BgjhService bgjhService) {
        this.bgjhService = bgjhService;
    }

    @Autowired
    public void setCgjsdService(CgjsdService cgjsdService) {
        this.cgjsdService = cgjsdService;
    }

    @Autowired
    public void setXsjsdService(XsjsdService xsjsdService) {
        this.xsjsdService = xsjsdService;
    }

    @Autowired
    public void setXsdService(XsdService xsdService) {
        this.xsdService = xsdService;
    }

    @Autowired
    public void setSkshService(SkshService skshService) {
        this.skshService = skshService;
    }

    @Autowired
    public void setFkshService(FkshService fkshService) {
        this.fkshService = fkshService;
    }

    @Autowired
    public void setKhService(KhService khService) {
        this.khService = khService;
    }

    @Autowired
    public void setXtggService(XtggService xtggService) {
        this.xtggService = xtggService;
    }

    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }

    @Autowired
    public void setJkdService(JkdService jkdService) {
        this.jkdService = jkdService;
    }

    @Autowired
    public void setBgCgdService(BgCgdService bgCgdService) {
        this.bgCgdService = bgCgdService;
    }

    @Autowired
    public void setCkshservice(CkshService ckshservice) {
        this.ckshservice = ckshservice;
    }

    @Autowired
    public void setInterfaceParam(InterfaceParam interfaceParam) {
        this.interfaceParam = interfaceParam;
    }
}
